package com.sanpu.kxe.business.main.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.ApplyCardOutput;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：办理信用卡
 * Created by 薛勇军 on 2018/8/3.
 */

public class HomeApplyCardAdapter extends BaseAdapter {

    private Context mContext;

    private List<ApplyCardOutput> mOutput = new ArrayList<>();

    private int maxColumns;//最大列数，用来限制一行显示,如果有需要显示多行或者个数不限的，maxColumns可以传最大的整型值

    public HomeApplyCardAdapter(Context mContext, List<ApplyCardOutput> mOutput, int maxColumns) {
        this.mContext = mContext;
        this.mOutput = mOutput;
        this.maxColumns = maxColumns;
    }

    public void setNotifyData(List<ApplyCardOutput> mOutput, int maxColumns) {
        this.mOutput = mOutput;
        this.maxColumns = maxColumns;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mOutput == null ? 0 : mOutput.size() >= maxColumns ? maxColumns : mOutput.size();
    }

    @Override
    public ApplyCardOutput getItem(int position) {
        if (mOutput == null)
            return null;
        return mOutput.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_home_apply_card, null);
        }
        ImageView ivBankCard = ButterKnife.findById(convertView, R.id.ivBankCard);
        ImageView ivHot = ButterKnife.findById(convertView, R.id.ivHot);

        View left = ButterKnife.findById(convertView, R.id.leftView);

        ApplyCardOutput retDatBean = getItem(position);
        if (retDatBean != null) {
            JKImageLoader.getInstance().displayImage(mContext,
                    UrlConstants.getBaseUrl()+retDatBean.getImage(), ivBankCard);
            if (retDatBean.isHot()) {
                ivHot.setVisibility(View.VISIBLE);
            } else {
                ivHot.setVisibility(View.INVISIBLE);
            }
        }

        if (position == 0) {
            left.setVisibility(View.VISIBLE);
        } else {
            left.setVisibility(View.GONE);
        }
        return convertView;
    }
}
